package com.ys.comapable;

import java.util.Objects;

/**
 * 描述: 学生类
 *
 * @author CodeYang
 * @version 2020/12/18 15:13
 */
public class Dog implements Comparable_diy ,Comparable{

    private String name;
    private int age;
    private double size;

    public Dog() {
    }

    public Dog(String name, int age, double size) {
        this.name = name;
        this.age = age;
        this.size = size;
    }

    public double getSize() {
        return size;
    }

    public void setSize(double size) {
        this.size = size;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Dog student = (Dog) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, size);
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", size=" + size +
                '}';
    }

    /**
     * 比较器--规则:
     * 根据 狗子的size进行排序-降序
     * <p>
     * this.age  >  student.age;返回 负数
     * this.age < student.age   返回 整数
     * this.age = student.age   返回 0
     *
     * @param obj
     * @return
     */
    @Override
    public int compareTo(Object obj) {
        // 1 判断 向下转型 使用本类数据  子类引用指向父类对象。
        if (obj instanceof Dog) {
            Dog dog = (Dog) obj;
            if (this.size > ((Dog) obj).size) {
                return -1;
            } else if (this.size < dog.size) {
                return 1;
            } else {
                return 0;
            }
        }
        throw new IllegalArgumentException("参数不合法");
    }
}
